package com.woniuxy.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 马宇航
 * @Description: topic主题模式
 * @DateTime: 22/10/21/0021 15:16
 * @Component: 成都蜗牛学苑
 **/
@Configuration
public class TopicConfig {
    @Bean
    public Queue topicQueue1(){
        return new Queue("topicQueue1");
    }
    @Bean
    public Queue topicQueue2(){
        return new Queue("topicQueue2");
    }
    @Bean
    public TopicExchange topicExchange(){
        //添加了额外绑定死信交换机的参数
        return new TopicExchange("topicExchange",true,false,deadArgs());
    }
    @Bean
    public Binding bindingTopic1(){
        //with就是bindingkey；它就是绑定交换机与队列的
        return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with("*.orange.*");
    }
    @Bean
    public Binding bindingTopic2(){
        //with就是bindingkey；它就是绑定交换机与队列的
        return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("*.*.rabbit");
    }
    @Bean
    public Binding bindingTopic3(){
        //with就是bindingkey；它就是绑定交换机与队列的
        return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("only.#");
    }
    //这个这个参数，是给正常交换机使用的，如果交换机找不到队列，直接使用备用交换机 fanoutExchange 通常备用交换机都是广播交换机
    public Map deadArgs(){
        Map<String,Object> otherArgs = new HashMap<>();
        otherArgs.put("alternate-exchange","fanoutExchange");
        return otherArgs;
    }
}
